package com.lucky.back.controller;


import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@Slf4j
@RestController
@Api(tags = "11.图片上传")
public class UploadController {

    @Value("${productdirPath}")
    private String productdirPath;

    @Value("${productTargetPath}")
    private String productTargetPath;

    @Value("${bannerdirPath}")
    private String bannerdirPath;

    @Value("${bannerTargetPath}")
    private String bannerTargetPath;

    @Value("${userdirPath}")
    private String userdirPath;

    @Value("${userTargetPath}")
    private String userTargetPath;

    private String filePath;


    public String upload(MultipartFile pic,String dirPath,String targetPath) throws IOException {
        log.debug(pic+"");
        //得到原始文件名
        String filename = pic.getOriginalFilename();
        log.debug("图片名"+filename);
        String suffix = filename.substring(filename.lastIndexOf("."));
        filename = UUID.randomUUID()+suffix;
        System.out.println(filename+"======================");
        System.out.println(suffix+"======================");
        File files=new File(dirPath);
        System.out.println("绝对路径："+files.getAbsolutePath());
        //准备文件夹
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //准备文件的完整路径
        filePath = files.getAbsolutePath()+"/"+filename;
        System.out.println(filePath + "======================");
    
        File file = new File(filePath);

        FileUtils.copyInputStreamToFile(pic.getInputStream(), file);


        files=new File(targetPath);
        System.out.println("绝对路径："+files.getAbsolutePath());
        //准备文件夹
        dirFile = new File(targetPath);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //准备文件的完整路径
        filePath = files.getAbsolutePath()+"/"+filename;
        System.out.println(filePath + "======================");

        file = new File(filePath);

        FileUtils.copyInputStreamToFile(pic.getInputStream(), file);

        return "/"+filename;
    }

    @RequestMapping("/remove")
    public void remove(String url,String dirPath){
        System.out.println("url = " + url);
        File files=new File(dirPath);
        filePath = files.getAbsolutePath();
        //删除文件
        new File(filePath+dirPath+url).delete();
    }

    @RequestMapping("productremove")
    public void productRemove(String url) throws IOException {
        String dirPath = productdirPath;
        remove(url, dirPath);
    }
    @RequestMapping("bannerremove")
    public void bannerRemove(String url) throws IOException {
        String dirPath = bannerdirPath;
        remove(url, dirPath);
    }
    @RequestMapping("userremove")
    public void bannertRemove(String url) throws IOException {
        String dirPath = userdirPath;
        remove(url, dirPath);
    }

    @RequestMapping("/productupload")
    public String productUpload(MultipartFile pic) throws IOException {
        String dirPath = productdirPath;
        String targetPath = productTargetPath;
        String path = upload(pic,dirPath,targetPath);
        return path;
    }

    @RequestMapping("/bannerupload")
    public String bannerUpload(MultipartFile pic) throws IOException {
        String dirPath = bannerdirPath;
        String targetPath = bannerTargetPath;
        String path = upload(pic,dirPath,targetPath);
        return path;
    }

    @RequestMapping("/userupload")
    public String userUpload(MultipartFile pic) throws IOException {
        String dirPath = userdirPath;
        String targetPath = userTargetPath;
        String path = upload(pic,dirPath,targetPath);
        return path;
    }


}
